from datetime import datetime,timezone
from app.extensions import db
from passlib.hash import pbkdf2_sha256 as sha256

class User(db.Model):
    __tablename__ = 'user'
    user_id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    username = db.Column(db.String(50), nullable=False, unique=True, comment='登录账号（手机号/邮箱）')
    password = db.Column(db.String(100), nullable=False, comment='加密后的密码')
    nickname = db.Column(db.String(50), default='用户' + str(user_id), comment='昵称（显示用）')  
    phone = db.Column(db.String(20), unique=True, comment='手机号')
    email = db.Column(db.String(100), unique=True, comment='邮箱')
    avatar_url = db.Column(db.String(255), comment='头像图片路径')  
    avatar_update_time = db.Column(db.DateTime, comment='头像更新时间')  
    gender = db.Column(db.String(10), default='保密', comment='性别（男/女/保密）')  
    birthday = db.Column(db.Date, comment='生日日期') 
    create_time = db.Column(db.DateTime, nullable=False, default=lambda: datetime.now(timezone.utc), comment='账号创建时间')
    update_time = db.Column(db.DateTime, nullable=False, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), comment='信息更新时间')

    # 密码加密
    @staticmethod
    def generate_hash(password):
        return sha256.hash(password)

    # 验证密码
    @staticmethod
    def verify_hash(password, hash):
        return sha256.verify(password, hash)